package com.event.management.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.event.management.backend.domain.Ticket;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 票种Mapper接口
 */
@Mapper
public interface TicketMapper extends BaseMapper<Ticket> {
    
    /**
     * 根据活动ID查询票种列表（未删除的）
     * @param eventId 活动ID
     * @return 票种列表
     */
    @Select("SELECT * FROM tickets WHERE event_id = #{eventId} AND is_deleted = 0")
    List<Ticket> findByEventId(@Param("eventId") Long eventId);
    
    /**
     * 检查活动是否存在
     * @param eventId 活动ID
     * @return 存在返回1，不存在返回0
     */
    @Select("SELECT COUNT(1) FROM events WHERE id = #{eventId} AND is_deleted = 0")
    int checkEventExists(@Param("eventId") Long eventId);
    
    /**
     * 获取活动状态
     * @param eventId 活动ID
     * @return 活动状态
     */
    @Select("SELECT status FROM events WHERE id = #{eventId} AND is_deleted = 0")
    String getEventStatus(@Param("eventId") Long eventId);
    
    /**
     * 检查用户是否是活动创建者或管理员
     * @param eventId 活动ID
     * @param userId 用户ID
     * @return 是创建者返回1，不是返回0
     */
    @Select("SELECT COUNT(1) FROM events WHERE id = #{eventId} AND creator_id = #{userId} AND is_deleted = 0")
    int checkIsEventCreator(@Param("eventId") Long eventId, @Param("userId") Long userId);
    
    /**
     * 统计总票种数量
     * @return 票种数量
     */
    @Select("SELECT COUNT(1) FROM tickets WHERE is_deleted = 0")
    Integer countTotalTickets();
    
    /**
     * 统计已售票数量
     * @return 已售票数量
     */
    @Select("SELECT SUM(sold_count) FROM tickets WHERE is_deleted = 0")
    Integer countSoldTickets();
    
    /**
     * 统计指定活动的已售票数量
     * @param eventId 活动ID
     * @return 已售票数量
     */
    @Select("SELECT SUM(sold_count) FROM tickets WHERE event_id = #{eventId} AND is_deleted = 0")
    Integer countSoldTicketsByEventId(@Param("eventId") Long eventId);
} 